package com.tecode.service;
import com.tecode.util.DateUtil;
import com.tecode.util.MessageUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

/**
 * Created by Administrator on 2019/4/4.
 */
@Service
public class MessageService {

    public void getVer(String phone) {
        //获取6位数的验证码
        int ver = (int) (Math.random() * 999999) + 100000;
        //发送短信的内容
        String message = "您的验证码为"+ver+"，请于1分钟内正确输入，如非本人操作，请忽略此短信。";
        //获取时间戳
        String timestamp = DateUtil.dateToString(new Date(),"yyytMMddHHmmss");
        //生成秒嘀签名
        String sign = sign(timestamp);
        //指定返回类型
        String respDataType = "JSON";
        //封装秒嘀请求参数
        String param = packagReqParam(phone, message, timestamp, sign, respDataType);
        //发送请求
        try {
            String res = send(param);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String send(String param) throws Exception {
        //1.封装请求地址
        URL url = new URL(MessageUtil.URL);
        //2.获取连接
        URLConnection urlConnection = url.openConnection();
        //3.设置连接超时时间以及是否可以输入和输出
        urlConnection.setConnectTimeout(3000);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        //4.获取输出流，并转换为字符流，加入缓冲流，并设置字符集编码为UTF-8
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
        //5.发送数据给秒滴
        bw.write(param);
        bw.flush();
        //6.获取输入流，并转换为字符流，加入缓冲流，并设置字符集编码为UTF-8
        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
        //7.获取秒滴的返回结果
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        bw.close();
        return sb.toString();
    }

    /**
     * 生成秒嘀平台签名
     * @param timestamp
     * @return
     */
    private String sign(String timestamp) {
        //1.获取到固定的accountSid和auth_token
        String accountSid = MessageUtil.ACCOUNTSID;
        String auth_token = MessageUtil.AUTHTOKEN;
        //2.使用MD5加密生成签名
        return DigestUtils.md5DigestAsHex((accountSid + auth_token + timestamp).getBytes());
    }

    /**
     * 封装秒嘀请求参数
     * @param phone
     * @param smsContent
     * @param timestamp
     * @param sig
     * @param respDataType
     * @return
     */
    private String packagReqParam(String phone, String smsContent, String timestamp, String sig, String respDataType) {
        StringBuilder sb = new StringBuilder();
        sb.append("accountSid=")
                .append(MessageUtil.ACCOUNTSID)
                .append("&smsContent=")
                .append(smsContent)
                .append("&to=")
                .append(phone)
                .append("&timestamp=")
                .append(timestamp)
                .append("&sig=")
                .append(sig)
                .append("&respDataType=")
                .append(respDataType);
        return sb.toString();
    }
}
